  <%@page import="org.json.simple.JSONArray"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@page import="com.crimax.lanproj.properties.LoanProperties"%>
<%@page contentType="text/html; charset=UTF-8"%>
  <%@page import="org.json.simple.JSONObject"%>
<%@  page
	import="com.crimax.lanproj.properties.LoanPropertiesHolder, java.io.StringWriter , java.util.List, com.crimax.lanproj.properties.PriceObject"%>
  <%
  	LoanPropertiesHolder holder = LoanPropertiesHolder.getSingletonObject();
      
       JSONObject obj=new JSONObject();
       

       //days=30&amount=1000
       String days = request.getParameter("days");
       String months = request.getParameter("months");
       String amount = request.getParameter("amount");
       String overview = request.getParameter("overview");
       
       
       if(months!=null){

           int intYears = new Integer(months).intValue();
           int intAmount = new Integer(amount).intValue();
           List<LoanProperties> sortedList = holder.getbigLoanSortSupplierByLoanSize(); 
           
           int count = 0;
	       while(count<5){
	    	   if (sortedList.size() > count) {
	    		   LoanProperties property = sortedList.get(count);
	    		   if(property.isQualifiedAmount(intAmount,intYears)){
		    		   	JSONObject result1 = new JSONObject();
				  		result1.put("name", property.getName());
				  		result1.put("friendlyName", property.getFriendlyName());
				  		result1.put("eleg", property.getEleg());
				  		result1.put("imgSrc", property.getImageSource());
				  		result1.put("weekEnd", property.getWeekendPayment());
				  		result1.put("uc", property.getUc());
				  		result1.put("epost", property.getEpost());
				  		result1.put("ba", property.getBa());
				  		result1.put("amount",amount );
				  		result1.put("url", property.getURL());
				  		
				  		result1.put("total", property.getBigTotal(intYears, intAmount));
		  				result1.put("fee", property.getBigFee(intYears, intAmount));
		  				result1.put("monthly", (int)property.getMonthlyPayment((double)intAmount,(double) intYears));
		  				result1.put("interest",property.getInterest());
				  		
				  		String resultName = "result"+(count+1);
				  		
				  		obj.put(resultName, result1);
	    		   }
			  		
	    	   }
	    	   count++;
	       }
       }
       
       
       if(days!=null){
        int intAmount = new Integer(amount).intValue();
        int intDays = new Integer(days).intValue();
        int indexedAmount = intAmount/1000;
        List<LoanProperties> sortedList = holder.getSortSupplierByLoanSize(intAmount, intDays);        
        
        int count = 0;
	        while(count<5){
			if (sortedList.size() > count) {
		  		JSONObject result1 = new JSONObject();
		  		result1.put("name", sortedList.get(count).getName());
		  		result1.put("friendlyName", sortedList.get(count).getFriendlyName());
		  		result1.put("eleg", sortedList.get(count).getEleg());
		  		result1.put("imgSrc", sortedList.get(count).getImageSource());
		  		result1.put("weekEnd", sortedList.get(count).getWeekendPayment());
		  		result1.put("uc", sortedList.get(count).getUc());
		  		result1.put("epost", sortedList.get(count).getEpost());
		  		result1.put("ba", sortedList.get(count).getBa());
		  		result1.put("amount",amount );
		  		result1.put("url", sortedList.get(count).getURL());
		  		
		  		String resultName = "result"+(count+1);
		
		  		if (intDays == 30) {
		  			List<PriceObject> price = sortedList.get(count).getPrice30();
		  			//check offer for this amount
		  			//System.out.println("p size  "+price.size());
						for (PriceObject priceObj : price) {
	
		  				if(priceObj.getAmount()==intAmount){
			  				result1.put("total", priceObj.getTotal());
			  				result1.put("fee", priceObj.getFee() );
			  				obj.put(resultName, result1);
		  				}
					}
		  		}
		
		  		if (intDays == 60) {
		  			List<PriceObject> price = sortedList.get(count).getPrice60();
		  			//check offer for this amount
		  			for (PriceObject priceObj : price) {
	
		  				if(priceObj.getAmount()==intAmount){
			  				result1.put("total", priceObj.getTotal());
			  				result1.put("fee", priceObj.getFee() );
			  				obj.put(resultName, result1);
		  				}
					}
		  		}
		
		  		if (intDays == 90) {
		  			List<PriceObject> price = sortedList.get(count).getPrice90();
		  			//check offer for this amount
					for (PriceObject priceObj : price) {
	
		  				if(priceObj.getAmount()==intAmount){
			  				result1.put("total", priceObj.getTotal());
			  				result1.put("fee", priceObj.getFee() );
			  				obj.put(resultName, result1);
		  				}
					}
		  			
		  		}
		  	}
			count++;
	    }
	}
       
       if(overview!=null){
           int intAmount = new Integer(amount).intValue();
           int intDays = new Integer(overview).intValue();
           int indexedAmount = intAmount/1000;
           List<LoanProperties> sortedList = holder.getSortSupplierByLoanSize(intAmount, intDays); 
		   
           JSONArray results = new JSONArray();
           
           for(LoanProperties prop : sortedList){
        	   JSONObject result1 = new JSONObject();
   		  		result1.put("name", prop.getName());
   		  	    result1.put("friendlyName", prop.getFriendlyName());
   		  		result1.put("eleg", prop.getEleg());
   		  		result1.put("imgSrc", prop.getImageSource());
   		  		result1.put("weekEnd", prop.getWeekendPayment());
   		  		result1.put("uc", prop.getUc());
   		  		result1.put("epost", prop.getEpost());
   		  		result1.put("ba", prop.getBa());
   		  		result1.put("amount",amount );
   		  	
   		  		
   		  		String resultName = "result";
   		
   		  		if (intDays == 30) {
   		  			List<PriceObject> price = prop.getPrice30();
   		  			//check offer for this amount
   		  			//System.out.println("p size  "+price.size());
   						for (PriceObject priceObj : price) {
   	
   		  				if(priceObj.getAmount()==intAmount){
   			  				result1.put("total", priceObj.getTotal());
   			  				result1.put("fee", priceObj.getFee() );
   			  			results.add(result1);
   		  				}
   					}
   		  		}
   		
   		  		if (intDays == 60) {
   		  			List<PriceObject> price = prop.getPrice60();
   		  			//check offer for this amount
   		  			for (PriceObject priceObj : price) {
   	
   		  				if(priceObj.getAmount()==intAmount){
   			  				result1.put("total", priceObj.getTotal());
   			  				result1.put("fee", priceObj.getFee() );
   			  			results.add(result1);
   		  				}
   					}
   		  		}
   		
   		  		if (intDays == 90) {
   		  			List<PriceObject> price = prop.getPrice90();
   		  			//check offer for this amount
   					for (PriceObject priceObj : price) {
   	
   		  				if(priceObj.getAmount()==intAmount){
   			  				result1.put("total", priceObj.getTotal());
   			  				result1.put("fee", priceObj.getFee() );
   			  			results.add(result1);
   		  				}
   					}
   		  			
   		  		}
   	    }
           obj.put("results", results);
           
   	}

  	out.print(obj);
  	out.flush();
  %>